#!/bin/sh
# Copyright (c) 2010 Tomas Carnecky
# Modify by (c) 2010 Tinctura Ginseng

tz='MSK-4MSK'
export TZ=$tz

BASE=/opt
#Print debug info
DEBUG=1
#Dest for debug messages
DEBUGFILE=$BASE/var/log/queue.log
LOCKFILE=$BASE/var/lock/queue.lock
SEEDFILE=$BASE/etc/seed.md5

[ -d $BASE/var/log ] || mkdir -p $BASE/var/log
[ -d $BASE/var/lock ] || mkdir -p $BASE/var/lock

#Stop completed torrents exept seeds (0 - don't stop anything)
STOPSEED=1
#Sort method. Uncomment only one string for your choice. Default - sort by ID, asc
#SORT="" #Sort by ID, asc
#SORT="-r -k 2" #Sort by complete, desc
#SORT="-k 2" #Sort by complete, asc
#SORT="-r -k 10" #Sort by name, desc
SORT="-k 10" #Sort by name, asc

#exit 0

[ $DEBUG -eq 0 ] || echo "[`date +\"%F %T\"`]: -= START =-" >> $DEBUGFILE

#Script not run at every hour 45 minutes
#M=`date +%M`
#M=$((M%45))
#if [ $M -eq 0 ]; then
#    [ $DEBUG -eq 0 ] || echo "[`date +\"%F %T\"`]: 45 minutes. Exiting" >> $DEBUGFILE
#    [ $DEBUG -eq 0 ] || echo "[`date +\"%F %T\"`]: -= STOP =-" >> $DEBUGFILE
#    exit 0
#fi

# *************
# Path to transmission-remote
PASS="torrent:1234"
REMOTE="/usr/local/etc/mos/trans/transmission-remote 127.0.0.1:9091 -n $PASS"

# Maximum number of torrents that may be active at any given time
MAXACTIVE="1"

[ $DEBUG -eq 0 ] || echo "[`date +\"%F %T\"`]: Check lock file" >> $DEBUGFILE
if [ -f $LOCKFILE ]; then
    [ $DEBUG -eq 0 ] || echo "[`date +\"%F %T\"`]: Found lock file" >> $DEBUGFILE
    D1=`date +%s -r $LOCKFILE`
    D2=`date +%s`
    D3=$((D2-D1))
    if [ $D3 -ge 1800 ] || [ $D3 -lt 0 ]; then
        [ $DEBUG -eq 0 ] || echo "[`date +\"%F %T\"`]: Lock file is staled. Remove it" >> $DEBUGFILE
        rm -f $LOCKFILE
    else
        [ $DEBUG -eq 0 ] || echo "[`date +\"%F %T\"`]: -= STOP =-" >> $DEBUGFILE
        exit 0
    fi
fi

[ $DEBUG -eq 0 ] || echo "[`date +\"%F %T\"`]: Create lock file" >> $DEBUGFILE
touch $LOCKFILE

# *************
# Stop all finished torrents
if [ $STOPSEED -ne 0 ]; then
    [ $DEBUG -eq 0 ] || echo "[`date +\"%F %T\"`]: Check all torrent for finished status" >> $DEBUGFILE
    LIST="$($REMOTE -l | grep -v "Status" | grep -v "^Sum\:" | grep 100% | grep -v Stopped | awk '{ print $1; }')"
    for ID in $LIST; do
        NAME="$($REMOTE -t $ID -i | grep Name:)"
        NAME="${NAME#*Name: }"
        SEED=`cat $SEEDFILE 2>/dev/null | grep "$NAME" | wc -l`
        if [ $SEED -eq 0 ]; then
            [ $DEBUG -eq 0 ] || echo "[`date +\"%F %T\"`]: Found torrent not for seed. Stop it" >> $DEBUGFILE
            $REMOTE -t $ID --stop >/dev/null
        fi
    done
fi

           
# How many are still running?
ACTIVE="$($REMOTE -l | grep -v "Status" | grep -v Stopped | grep -v 100% | grep -v "^Sum\:" | wc -l)"
if [ $ACTIVE -ge $MAXACTIVE ]; then
    [ $DEBUG -eq 0 ] || echo "[`date +\"%F %T\"`]: Maximum or great active torrents. Exiting" >> $DEBUGFILE
    [ $DEBUG -eq 0 ] || echo "[`date +\"%F %T\"`]: Remove lock file" >> $DEBUGFILE
    rm -f $LOCKFILE
    [ $DEBUG -eq 0 ] || echo "[`date +\"%F %T\"`]: -= STOP =-" >> $DEBUGFILE
    exit 0
fi
                
[ $DEBUG -eq 0 ] || echo "[`date +\"%F %T\"`]: Check for torrent who need to leech" >> $DEBUGFILE
# Start new torrents
LIST="$($REMOTE -l | grep -v "Status" | grep -v 100% | grep Stopped | sort $SORT | head -n $(expr $MAXACTIVE - $ACTIVE) | awk '{ print $1; }')"
for ID in $LIST; do
    NAME="$($REMOTE -t $ID -i | grep Name:)"
    $REMOTE -t $ID --start >/dev/null
    [ $DEBUG -eq 0 ] || echo "[`date +\"%F %T\"`]: Start torrent for leech" >> $DEBUGFILE
done

[ $DEBUG -eq 0 ] || echo "[`date +\"%F %T\"`]: Remove lock file" >> $DEBUGFILE
rm -f $LOCKFILE

[ $DEBUG -eq 0 ] || echo "[`date +\"%F %T\"`]: -= STOP =-" >> $DEBUGFILE
